package com.cskaoyan.controller;

import com.cskaoyan.bean.data.CartCheckoutData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.data.CartIndexData;
import com.cskaoyan.bean.vo.CartAddVo;
import com.cskaoyan.bean.vo.CartCheckedVo;
import com.cskaoyan.model.MarketCart;
import com.cskaoyan.service.CartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Date;

/**
 * @Description
 * @Author zoe ling
 * @Date 2024/6/12
 */
@RestController
@RequestMapping("wx/cart")
public class WxCartController {
    @Autowired
    CartService cartService;

    @GetMapping("goodscount")
    public BaseRespVo goodscount(){
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            //根据id在marketcart表中查询指定用户id对应的商品数量
            long goodscount=cartService.goodscount(userId);
            return BaseRespVo.ok(goodscount);
        } else {
            return BaseRespVo.unAuthc();
        }


    }
    @PostMapping("add")
    public BaseRespVo add(@RequestBody CartAddVo cartAddVo){
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            MarketCart marketCart=cartService.find(userId,cartAddVo.getGoodsId(),cartAddVo.getProductId());
            if (marketCart==null){
                cartService.create(cartAddVo,userId);
            }else {
                int newNumber = marketCart.getNumber().intValue() + cartAddVo.getNumber().intValue();
                MarketCart updateCart = new MarketCart();
                updateCart.setId(marketCart.getId());
                updateCart.setNumber((short) newNumber);
                updateCart.setUpdateTime(new Date());
                cartService.modify(updateCart);
            }
            return BaseRespVo.ok("success");

        } else {
            return BaseRespVo.unAuthc();
        }

    }
    @GetMapping("index")
    public BaseRespVo index(){
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            CartIndexData data=cartService.index(userId);
            return BaseRespVo.ok(data);
        } else {
            return BaseRespVo.unAuthc();
        }
    }
    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody CartCheckedVo cartCheckedVo){
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            boolean flag=cartService.updateChecked(userId,cartCheckedVo.getProductIds(),cartCheckedVo.getIsChecked());

            CartIndexData data = cartService.index(userId);
            return flag==true?BaseRespVo.ok(data):BaseRespVo.ok(data);
        } else {
            return BaseRespVo.unAuthc();
        }


    }
    @PostMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody CartAddVo cartFastaddVo){
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            MarketCart marketCart= cartService.selectByGoodsId(userId,cartFastaddVo);
            if (marketCart!=null){
                return BaseRespVo.ok(marketCart.getId());
            }else {
                //如果购物车没有记录
                MarketCart marketCartnew =cartService.getCartId(userId,cartFastaddVo);
                Integer id = marketCartnew.getId();
                return BaseRespVo.ok(id);
            }

        } else {
            return BaseRespVo.unAuthc();
        }
    }

    @GetMapping("checkout")
    public BaseRespVo checkout(Integer cartId,Integer addressId,Integer couponId,Integer userCouponId ){
        //根据购物车选中的商品计算要生成的订单信息
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            CartCheckoutData data=cartService.checkout(userId,cartId,addressId,couponId,userCouponId);
            return BaseRespVo.ok(data);
        } else {
            return BaseRespVo.unAuthc();
        }

    }

}
